# -*- coding: utf-8 -*-
# @Time : 2021/12/24 13:46
# @Author : Limusen
# @File : regular_26

import re
import jsonpath

# 　示例代码
num_list = [1, 2, 3, 4]
name_list = ["one", "two", "three", "four"]
# print({name_list[i]: num_list[i] for i in range(len(num_list))})

print('#######单个值替换#######')
request_info = {'测试用例编号': 'api_cese_01', '测试用例名称': '获取用户信息', '用例执行': '是', '用例步骤': 'step_02', '接口名称': '获取用户信息',
                '请求方式': 'get',
                '请求地址': '/gateway/system/user/userInfo/oneself', '请求参数(get)': '', '请求参数(post)': '', '取值方式': '正则取值',
                '取值代码': '"userId":"(.+?)"', '取值变量': 'userid',
                '断言类型': 'json_key_value', '期望结果': '{"code":"100001"}'}
value = '"data":"{"测试用例编号":"api_cese_01","请求参数(get)": "","userId":"测试奥什","phone":"17717870595","取值变量": "userid,nick_name","nickName":"恭喜发财"}"'

# 取单个值
temp_variables = {}
if request_info["取值方式"] == "正则取值":
    value_list = request_info['取值代码'].split(',')
    key_list = request_info['取值变量'].split(',')
    for c in range(0, len(value_list)):  # 取出总共需要替换的次数，每次循环都一次替换两个值
        temp_variables[key_list[c]] = re.findall(value_list[c], value)[0]
print(temp_variables)
print('#######单个值替换#######')

print("\n")

print('#######多个值替换#######')
request_info = {'测试用例编号': 'api_cese_01', '测试用例名称': '获取用户信息', '用例执行': '是', '用例步骤': 'step_02', '接口名称': '获取用户信息',
                '请求方式': 'get',
                '请求地址': '/gateway/system/user/userInfo/oneself', '请求参数(get)': '', '请求参数(post)': '', '取值方式': '正则取值',
                '取值代码': '"userId":"(.+?)","测试用例编号":"(.+?)"', '取值变量': 'userid,case_id',
                '断言类型': 'json_key_value', '期望结果': '{"code":"100001"}'}
value = '"data":"{"测试用例编号":"api_cese_01","请求参数(get)": "","userId":"测试奥什","phone":"17717870595","取值变量": "userid,nick_name","nickName":"恭喜发财"}"'

# 　取多个值
temp_variables = {}
if request_info["取值方式"] == "正则取值":
    value_list = request_info['取值代码'].split(',')
    key_list = request_info['取值变量'].split(',')
    for c in range(0, len(value_list)):
        temp_variables[key_list[c]] = re.findall(value_list[c], value)[0]
print(temp_variables)
print('#######多个值替换#######')

print('\n')

print('#######优化替换正则模块#######')

result_list = '{"code": 100001,"desc": "成功","success": "true","data": "{"token": "I0k",  "activation":"true"}"}'
result_json = {"code": "100001", "desc": "成功", "success": "true", "data": {"token": "I0k", "activation": "true"}}

request_info = {'测试用例编号': 'api_cese_01', '测试用例名称': '获取用户信息', '用例执行': '是', '用例步骤': 'step_02', '接口名称': '获取用户信息',
                '请求地址': '/gateway/system/user/userInfo/oneself', '请求参数(get)': '', '请求参数(post)': '',
                '取值方式': '正则取值',
                '取值代码': '"code": (\d+),"success": "(.+?)"', '取值变量': 'code,success',
                '断言类型': '正则取值', '期望结果': '{"code":"100001"}'}
temp_variables = {}
if request_info["取值方式"] == "正则取值":
    # 注意：取值代码与取值变量需要对应，否则会报错，用逗号分割数据
    value_list = request_info['取值代码'].split(',')
    key_list = request_info['取值变量'].split(',')
    for index in range(0, len(value_list)):
        # 如果这个地方报错了，请查看正则表达式或者jsonpath取值模板是否有问题
        # 这里需要注意的是 取出字符串数据,中间是没有空格的
        # 例如"code": "100001"  正则模板应该为 "code":"(.+?)"
        temp_variables[key_list[index]] = re.findall(value_list[index], result_list)[0]
print(temp_variables)

print('#######优化替换正则模块#######')

print('#######单个值替换#######')
request_info = {'测试用例编号': 'api_cese_01', '测试用例名称': '获取用户信息', '用例执行': '是', '用例步骤': 'step_02', '接口名称': '获取用户信息',
                '请求方式': 'get',
                '请求地址': '/gateway/system/user/userInfo/oneself', '请求参数(get)': '', '请求参数(post)': '',
                '取值方式': 'jsonpath取值',
                '取值代码': '$..userId', '取值变量': 'userId',
                '断言类型': 'json_key_value', '期望结果': '{"code":"100001"}'}
json_path_list = {
    "access_token": "52_obqRF34JqB59FMSh",
    "expires_in": 7200,
    "sads": 545,
    "data": {"测试用例编号": "api_cese_01", "userId": "张二狗", "phone": "17717870595"}
}
# 取单个值
temp_variables = {}
if request_info["取值方式"] == "jsonpath取值":
    value_list = request_info['取值代码'].split(',')
    key_list = request_info['取值变量'].split(',')
    for index in range(0, len(value_list)):
        temp_variables[key_list[index]] = jsonpath.jsonpath(json_path_list, value_list[index])[0]
print(temp_variables)
print('#######单个值替换#######')

print('#######多个值替换#######')
request_info = {'测试用例编号': 'api_cese_01', '测试用例名称': '获取用户信息', '用例执行': '是', '用例步骤': 'step_02', '接口名称': '获取用户信息',
                '请求方式': 'get',
                '请求地址': '/gateway/system/user/userInfo/oneself', '请求参数(get)': '', '请求参数(post)': '',
                '取值方式': 'jsonpath取值',
                '取值代码': '$..userId,$.sads', '取值变量': 'userId,sads',
                '断言类型': 'json_key_value', '期望结果': '{"code":"100001"}'}
json_path_list = {
    "access_token": "52_obqRF34JqB59FMSh",
    "expires_in": 7200,
    "sads": 545,
    "data": {"测试用例编号": "api_cese_01", "userId": "张二狗", "phone": "17717870595"}
}
# 取单个值
temp_variables = {}
if request_info["取值方式"] == "jsonpath取值":
    value_list = request_info['取值代码'].split(',')
    key_list = request_info['取值变量'].split(',')
    for index in range(0, len(value_list)):
        temp_variables[key_list[index]] = jsonpath.jsonpath(json_path_list, value_list[index])[0]
print(temp_variables)
print('#######多个值替换#######')

print('#######优化替换jsonpath模块#######')

result_json = {"code": "100001", "desc": "成功", "success": "true", "data": {"token": "I0k", "activation": "true"}}
request_info = {'测试用例编号': 'api_cese_01', '测试用例名称': '获取用户信息', '用例执行': '是', '用例步骤': 'step_02', '接口名称': '获取用户信息',
                '请求地址': '/gateway/system/user/userInfo/oneself', '请求参数(get)': '', '请求参数(post)': '',
                '取值方式': 'jsonpath取值',
                '取值代码': '$.code,$..token', '取值变量': 'code,token',
                '断言类型': 'jsonpath取值', '期望结果': '{"code":"100001"}'}
temp_variables = {}
if request_info["取值方式"] == "jsonpath取值":
    value_list = request_info['取值代码'].split(',')
    key_list = request_info['取值变量'].split(',')
    for index in range(0, len(value_list)):
        temp_variables[key_list[index]] = jsonpath.jsonpath(result_json, value_list[index])[0]
print(temp_variables)

print('#######优化替换jsonpath模块#######')

print('####### 整合替换request_utils.py模块 #######')

result_list = '{"code": 100001,"desc": "成功","success": "true","data": "{"token": "I0k",  "activation":"true"}"}'
result_json = {"code": "100001", "desc": "成功", "success": "true", "data": {"token": "I0k", "activation": "true"}}

request_info = {'测试用例编号': 'api_cese_01', '测试用例名称': '获取用户信息', '用例执行': '是', '用例步骤': 'step_02', '接口名称': '获取用户信息',
                '请求地址': '/gateway/system/user/userInfo/oneself', '请求参数(get)': '', '请求参数(post)': '',
                '取值方式': '正则取值',
                '取值代码': '"code": (\d+)', '取值变量': 'code',
                '断言类型': '正则取值', '期望结果': '{"code":"100001"}'}
temp_variables = {}
if request_info["取值方式"] == "正则取值":
    value_list = request_info['取值代码'].split(',')
    key_list = request_info['取值变量'].split(',')
    for index in range(0, len(value_list)):
        # 如果这个地方报错了，请查看正则表达式或者jsonpath取值模板是否有问题
        # 这里需要注意的是 取出字符串数据,中间是没有空格的
        # 例如"code": "100001"  正则模板应该为 "code":"(.+?)"
        temp_variables[key_list[index]] = re.findall(value_list[index], result_list)[0]
elif request_info["取值方式"] == "jsonpath取值":
    value_list = request_info['取值代码'].split(',')
    key_list = request_info['取值变量'].split(',')
    for index in range(0, len(value_list)):
        temp_variables[key_list[index]] = jsonpath.jsonpath(result_json, value_list[index])[0]
print(temp_variables)

print('####### 整合替换request_utils.py模块 #######')
